*** Clientelism as Persuasion: Evidence from Latin America ***
*** Authors: Joby Schaffer and Andy Baker ***
*** Dataset: "Mexico 2006 Panel Dataset" available here http://web.mit.edu/clawson/www/polisci/research/mexico06/
use "C:\Users\Andy baker\OneDrive - UCB-O365\Data\Mexico\Mexico 2006\base panel mexico 2006.dta", clear

*I am downweighting the states that were oversampled, including df
gen pweight=1
replace pweight=160/	(160+500) if estado==9
replace pweight=80/(80+140) if estado==7
replace pweight=100/(80+100) if estado==14
replace pweight=60/(80+60) if estado==20	
svyset [pweight=pweight], psu(municipi)

*--------------------------------------------------------------------------- 
*Clientelism Variable 
*Pre-Election (May) Wave
gen client_2=p42_2
replace client_2=. if client_2==3
recode client_2 (1=1) (2=0)

*Post-Election (July) Wave
gen client_3=p29_3
replace client_3=. if client_3==3 
recode client_3 (1=1) (2=0)

*--------------------------------------------------------------------------
****Network Measures:

*Network Size
*Pre-Election (May) Wave
egen netsize_2=rowtotal(p66a_2 p66b_2 p66c_2)
replace netsize_2=. if munic_2==.

*Post-Election (July) Wave
egen netsize_3=rowtotal(p48a_3 p48b_3 p48c_3)
replace netsize_3=. if munic_2==.

*Type of Network --> 1=Insular (Family); 0=Outward (non-Family)
*Wave 2 - final variable to be used is called fam_w2
gen family1=p67a_2
recode family1 (1=1) (2=1)(3=0)(4=1)(5=0)(6=0)(7=0)(8=0)(9=0)
gen family2=p67b_2
recode family2 (1=1) (2=1)(3=0)(4=1)(5=0)(6=0)(7=0)(8=0)(9=0)
gen family3=p67c_2
recode family3 (1=1) (2=1)(3=0)(4=1)(5=0)(6=0)(7=0)(8=0)(9=0)
	egen t=rowtotal(family1 family2 family3)
	gen family_w2=t
	replace family_w2=. if munic_2==.
	
*Wave 3 - final variable to be used is called fam_w3 
drop t family1 family2 family3
gen family1=p49a_3
recode family1 (1=1) (2=1)(3=0)(4=1)(5=0)(6=0)(7=0)(8=0)(9=0)
gen family2=p49b_3
recode family2 (1=1) (2=1)(3=0)(4=1)(5=0)(6=0)(7=0)(8=0)(9=0)
gen family3=p49c_3
recode family3 (1=1) (2=1)(3=0)(4=1)(5=0)(6=0)(7=0)(8=0)(9=0)
	egen t=rowtotal(family1 family2 family3)
	gen family_w3=t
	replace family_w3=. if munic_3==.
	drop t

*Type of Network --> Count of non-family members
	*Wave 2 
gen nonfamily1_w2=p67a_2
recode nonfamily1_w2 (1=0) (2=0)(3=1)(4=0)(5=1)(6=1)(7=1)(8=1)(9=0)
gen nonfamily2_w2=p67b_2
recode nonfamily2_w2 (1=0) (2=0)(3=1)(4=0)(5=1)(6=1)(7=1)(8=1)(9=0)
gen nonfamily3_w2=p67c_2
recode nonfamily3_w2 (1=0) (2=0)(3=1)(4=0)(5=1)(6=1)(7=1)(8=1)(9=0)
egen nonfamily_w2=rowtotal(nonfamily1_w2 nonfamily2_w2 nonfamily3_w2)
replace nonfamily_w2=. if munic_2==.
	
	*Wave 3 
gen nonfamily1_w3=p49a_3
recode nonfamily1_w3 (1=0) (2=0)(3=1)(4=0)(5=1)(6=1)(7=1)(8=1)(9=0)
gen nonfamily2_w3=p49b_3
recode nonfamily2_w3 (1=0) (2=0)(3=1)(4=0)(5=1)(6=1)(7=1)(8=1)(9=0)
gen nonfamily3_w3=p49c_3
recode nonfamily3_w3 (1=0) (2=0)(3=1)(4=0)(5=1)(6=1)(7=1)(8=1)(9=0)
egen nonfamily_w3=rowtotal(nonfamily1_w3 nonfamily2_w3 nonfamily3_w3)
replace nonfamily_w3=. if munic_3==.

sort folio
merge folio using "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Publications Old\Paper Schaffer\Mexico\discussants.dta"

*Create variable for when wave 3 discussant was named in wave 2. Equals zero if not mentioned in wave 2
rename discussant1 disc_w3_when_mentioned_in_w2_1
rename discussant2 disc_w3_when_mentioned_in_w2_2
rename discussant3 disc_w3_when_mentioned_in_w2_3

*Create binary variable of whether wave 3 discussant was mentioned in wave 2
gen disc_w2_mentioned_in_w3_1=1 if disc_w3_when_mentioned_in_w2_1==1 | disc_w3_when_mentioned_in_w2_2==1 | disc_w3_when_mentioned_in_w2_3==1
gen disc_w2_mentioned_in_w3_2=1 if disc_w3_when_mentioned_in_w2_1==2 | disc_w3_when_mentioned_in_w2_2==2 | disc_w3_when_mentioned_in_w2_3==2
gen disc_w2_mentioned_in_w3_3=1 if disc_w3_when_mentioned_in_w2_1==3 | disc_w3_when_mentioned_in_w2_2==3 | disc_w3_when_mentioned_in_w2_3==3

replace nonfamily1_w2=0 if disc_w2_mentioned_in_w3_1==1
replace nonfamily2_w2=0 if disc_w2_mentioned_in_w3_2==1
replace nonfamily3_w2=0 if disc_w2_mentioned_in_w3_3==1
egen nonfamily_w2_nonrepeats=rowtotal(nonfamily1_w2 nonfamily2_w2 nonfamily3_w2)
replace nonfamily_w2_nonrepeats=. if munic_3==.


*gen family_both=family_w2+family_w3
gen nonfamily_both=nonfamily_w2_nonrepeats+nonfamily_w3

*------------------------------------------------------------------------------------------------------------
*Other Recodes
*
*Party ID variables - p22; p23; p24; p25; p26
	*Wave 1
*Ordinal - Non-partisan; weak partisan; strong partisan. 
gen partisan_w1 = p22
recode partisan_w1 (1=2)(2=1)(3=2)(4=1)(5=2)(6=1)(7=1)(8=0)(9=0)

	*Wave 2
*Ordinal - Non-partisan; weak partisan; strong partisan. 
gen partisan_w2 = p22_2
recode partisan_w2 (1=2)(2=1)(3=2)(4=1)(5=2)(6=1)(7=1)(8=0)(9=0)
	
	*Wave 3
*Ordinal - Non-partisan; weak partisan; strong partisan. 
gen partisan_w3 = p19_3
recode partisan_w3 (1=2)(2=1)(3=2)(4=1)(5=2)(6=1)(7=1)(8=0)(9=0)

*Education (high= more education)
gen educ_w2=p74_2
replace educ=. if educ==10

*Income
recode  p64a p64b p64c p64d p64e p64f p64g p64h (2=0)
egen missing=rowmiss(  p64a p64b p64c p64d p64e p64f p64g p64h)
egen wealthtemp=rowtotal(  p64a p64b p64c p64d p64e p64f p64g p64h)
gen wealth_w1=wealthtemp/(8-missing)

*Gender (0,1)
gen female=a-1

*Type of place - rural, mixed, urban
gen placetype = tipo
recode placetype (3=1)(2=0)(1=2)

*Political Interest (high = high interest)
gen polint_w1=p2-1
replace polint_w1=. if polint_w1==4
recode polint_w1 (0=3)(1=2)(2=1)(3=0)

gen polint_w2=p2_2-1
replace polint_w2=. if polint_w2==4
recode polint_w2 (0=3)(1=2)(2=1)(3=0)

gen polint_w3=p2_3-1
replace polint_w3=. if polint_w3==4
recode polint_w3 (0=3)(1=2)(2=1)(3=0)

recode c3 0=.
gen age=2006-c3

*Association
gen sports=p16a_3
recode sports (1=2)(2=1)(3=0)(4=0)
gen pta=p16b_3
recode pta (1=2)(2=1)(3=0)(4=0)
gen neighborhood=p16c_3
recode neighborhood (1=2)(2=1)(3=0)(4=0)
gen prof=p16g_3
recode prof (1=2)(2=1)(3=0)(4=0)
gen environ=p16f_3
recode environ (1=2)(2=1)(3=0)(4=0)
gen party=p16d_3
recode party (1=2)(2=1)(3=0)(4=0)

egen assoc=rowtotal(sports pta neighborhood environ prof)

gen responded2and3=1 if munic_2~=. & munic_3~=.
drop if responded2and3~=1
drop if client_2==. | client_3==.

*Discussants and targeting (Preliminary, listwise deletion results)
/*logit client_3 client_2 nonfamily_w2 family_w2 partisan_w1 partisan_w2 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party if responded2and3==1
logit client_3 client_2 nonfamily_w2 family_w2 partisan_w3 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party if responded2and3==1

logit client_3 nonfamily_w2 family_w2 partisan_w1 partisan_w2 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party if responded2and3==1
logit client_3 nonfamily_w2 family_w2 partisan_w3 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party if responded2and3==1

estsimp logit client_3 client_2 nonfamily_w2 family_w2 partisan_w2 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party if responded2and3==1, genname(bz)
setx client_2 0 nonfamily_w2 0 family_w2 0 (partisan_w2 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party) mean
simqi
setx client_2 0 nonfamily_w2 3 family_w2 0 (partisan_w2 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party) mean
simqi
setx client_2 0 nonfamily_w2 0 family_w2 3 (partisan_w2 polint_w2 wealth_w1 educ_w2 age female placetype sports pta neighborhood prof environ party) mean
simqi
*/

*Models referenced in footnote 
oprobit partisan_w3 partisan_w2 client_3 client_2 nonfamily_w3 nonfamily_w2 polint_w3 polint_w2 wealth_w1 educ_w2 age female placetype if responded2and3==1
poisson nonfamily_w3 nonfamily_w2 client_3 client_2 partisan_w3 partisan_w2 polint_w3 polint_w2 wealth_w1 educ_w2 age female placetype if responded2and3==1
svy: mean partisan_w1 partisan_w2 partisan_w3 if responded2and3==1, over(client_3)
svy: mean nonfamily_w2 nonfamily_w3 if responded2and3==1, over(client_3)


*Table A.20 results
eststo clear
eststo: svy: logit client_3 nonfamily_w2 family_w2 partisan_w1  //Model 1
margin, at(nonfamily_w2==0 family_w2=0) atmeans 
margin, at(nonfamily_w2==3 family_w2=0) atmeans 
eststo: svy: logit client_3 nonfamily_w2 family_w2 partisan_w2 //Model 2
eststo: svy: logit client_3 nonfamily_w2 family_w2 partisan_w3 //Model 3

*Figure 7.4
coefplot (est1, label(Model 1) mcolor(black) ciopts(lcolor(black) recast(rcap)))  ///
(est2, label(Model 2) mcolor(black) msymbol(S) ciopts(lcolor(black) recast(rcap)))  ///
(est3, label(Model 3) mcolor(black) msymbol(t) msize(medlarge) ciopts(lcolor(black) recast(rcap))) , ///
drop(*.* _cons) xline(0, lcolor(black)) yline(1.5 2.5 3.5 4.5 5.5, lcolor(black)) ///
legend(cols(1)) graphregion(color(white)) order(nonfamily_w2 family_w2 polint_w2 partisan_w1 partisan_w2 partisan_w3 ) plotregion(lstyle(yxline) lcolor(black)) ///
coeflabel(nonfamily_w2="No. of nonfamilial alters" family_w2="No. of familial alters"  ///
partisan_w1="Strength.of.partisanship{sub:{it:pc}}" partisan_w2="Strength.of.partisanship{sub:{it:c}}" partisan_w3="Strength.of.partisanship{sub:{it:e}}", wrap(42))  ///
msize(small) xtitle("{bf:Logit Coefficient}") ytitle("{bf:Independent Variables}")  ///
title("{bf:Dependent Variable: Paid off by a Party}", size(medsmall) color(black)) 

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 7 Clientelism\Figures\Mexico2006clientregs.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR7_4.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR7_4.pdf", as(pdf) replace 

xyz


*polint_w2 pta neighborhood prof environ sports party wealth_w1 educ_w2 female age placetype 


*Models referenced in footnote
mim: oprobit partisan_w3 partisan_w2 client_3 client_2 nonfamily_w3 nonfamily_w2 polint_w3 polint_w2 wealth_w1 educ_w2 age female placetype 
mim: poisson nonfamily_w3 nonfamily_w2 client_3 client_2 partisan_w3 partisan_w2 polint_w3 polint_w2 wealth_w1 educ_w2 age female placetype 

